<?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Date: 2017/1/19
 * Time: 18:21
 */
namespace App\Repositories;
use App\Models\TestingVisit;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
use Cache;
use App\Models\LogHelper;
class TestingVisitRepository extends Repository
{
    protected $tag = 'testing.visit';
    /**redis存储数据形成消息队列
     * @param $uid
     * @param $tid
     * @param $type
     * @param $listKey
     * @return int   成功：执行 RPUSH操作后，返回列表的长度
     */
    public function createRedisList($uid,$tid,$type,$listKey)
    {
        $time = time();
        $message = [
            'uid' => $uid,
            'tid' => $tid,
            'type' => $type,
            'time' => $time,
            'ip'=>getIp(),
            'created_at'=>date('Y-m-d H:i:s', $time),
        ];
        $rPushResul = Redis::rPush($listKey, json_encode($message));
        return $rPushResul;
    }

    /**读取redis消息队列里面的数据，批量入库
     * 消息Redis方法保存到Mysql数据库
     * @param $listKey
     */
   public function RedisSaveToMysql($listKey)
    {
        if(Redis::exists($listKey)){
            $redisInfo  =Redis::lRange($listKey, 0, -1);
            $dataLength =Redis::lLen($listKey);
            $arrList = [];
            if ($dataLength > 0) {
                try {
                    Redis::watch($listKey);
                    foreach ($redisInfo as $key => $val) {
                        $arrList[$key] = array(
                            'uid' => json_decode($val, true)['uid'],
                            'tid' => json_decode($val, true)['tid'],
                            'type' => json_decode($val, true)['type'],
                            'time' => json_decode($val, true)['time'],
                            'ip' => json_decode($val, true)['ip'],
                            'created_at'=>json_decode($val, true)['created_at'],
                        );
                    }
                    TestingVisit::insert($arrList);
                    Redis::lTrim($listKey, -1, 0);
                } catch (Exception $e) {
                    LogHelper::log('REDIS_VISIT'.'====>'.$e->getMessage());
                }
            }
        }
    }

    public function getPv($tid){
        return TestingVisit::where('tid',$tid)->count();
        }

    public function getUv($tid){
         //$res = DB::select('select uid from testing_visit where tid = :tid and to_days(created_at)=to_days(now())',['tid'=>$tid]);
        $res = DB::select('select distinct uid from testing_visit where tid = :tid and to_days(created_at)=to_days(now())',['tid'=>$tid]);
        if($res){
            return  count($res);
        }else{
            return  0;
        }
    }

    public function getIp($tid){
        $res = DB::select('select distinct ip from testing_visit where tid = :tid and to_days(created_at)=to_days(now())',['tid'=>$tid]);
        if($res){
            return  count($res);
        }else{
            return  0;
        }
    }


    public function cacheTime()
    {
        return config('testing.common_cache_time');
    }


    public function model()
    {
        return app(TestingVisit::class);
    }

    public function tag()
    {
        return self::$tag;
    }

}